package com.haitaoit.chengquan.module.center.activity.info.update;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BaseActivity;
import com.haitaoit.chengquan.module.center.model.UserInfoModel;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.utils.GetSign;
import com.haitaoit.chengquan.utils.HttpUtilsSingle;
import com.haitaoit.chengquan.utils.NameValuePairSign;
import com.haitaoit.chengquan.utils.PreferenceUtils;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.haitaoit.chengquan.utils.Utils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class UpdatePhoneActivity3 extends BaseActivity {
    @ViewInject(R.id.updatephone_phone)
    EditText updatephone_phone;
    @ViewInject(R.id.updatephone_code)
    EditText updatephone_code;
    @ViewInject(R.id.updatephone_getcode)
    TextView updatephone_getcode;


    private int time;
    private Timer timer = new Timer();
    private TimerTask wait;
    private String code = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_update_phone3);
        setTitle_V("更换手机号");
        setLeftShow(1, R.mipmap.left);
        backLeft_V();
        ViewUtils.inject(this);

    }

    @OnClick({R.id.updatephone_getcode, R.id.phone_next3,})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.updatephone_getcode:
                if (updatephone_getcode.getText().toString().equals("获取验证码")) {
                    if (Utils.isMobileNum(updatephone_phone.getText().toString())) {
                        httpGetCode();
                        wait = new TimerTask() {
                            @Override
                            public void run() {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (time <= 0) {
                                            updatephone_getcode.setEnabled(true);
                                            updatephone_getcode.setFocusable(true);
                                            updatephone_getcode.setFocusableInTouchMode(true);
                                            updatephone_getcode.requestFocus();

                                            updatephone_getcode.setText("获取验证码");
                                            updatephone_phone.setEnabled(true);
                                            wait.cancel();
                                        } else {
                                            updatephone_getcode.setEnabled(false);
                                            updatephone_getcode.setFocusable(false);
                                            updatephone_getcode.setFocusableInTouchMode(false);
                                            updatephone_getcode.setText(time + "秒后重新获取");
                                        }
                                        time--;
                                    }
                                });
                            }
                        };
                        time = 60;
                        timer.schedule(wait, 0, 1000);
                    } else {
                        ToastUtils.showToast("手机格式有误请重新输入！");
                    }
                }
                break;
            case R.id.phone_next3:
                if (code.equals("")) {
                    ToastUtils.showToast("请先获取验证码！");
                } else if (!updatephone_code.getText().toString().equals(code)) {
                    ToastUtils.showToast("验证码有误，请重新输入！");
                } else {
                    httpGetupdate_phone();
                }
                break;
        }
        super.onClick(v);
    }

    private void httpGetupdate_phone() {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("phone", updatephone_phone.getText().toString()));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("phone", updatephone_phone.getText().toString());
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getupdate_phone, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                JSONObject json;
                try {
                    json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            ToastUtils.showToast(Errmsg);
                            UserInfoModel.getUser_info().setPhone(updatephone_phone.getText().toString());
                            Intent in = new Intent();
                            in.setAction("updatephone_succ");
                            UpdatePhoneActivity3.this.sendBroadcast(in);
                            finish();
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }

    // 获取手机验证码
    private void httpGetCode() {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("phone", updatephone_phone.getText().toString()));
        list.add(new NameValuePairSign("type", "2"));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("phone", updatephone_phone.getText().toString());
        params.addQueryStringParameter("type", "2");
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getupdate_User_phonecode, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                try {
                    JSONObject json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            time = 0;
                            return;
                        case 0:// 成功
                            JSONObject js = new JSONObject(Response);
                            code = js.getString("code");
                            updatephone_phone.setEnabled(false);
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                time = 0;
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }
}
